package ifosup.seb.calto;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Calculatrice {
	
	private JFrame frame = new JFrame();

	private JTextField textField = new JTextField(14);
	
	private JPanel container = new JPanel();
	
	private ArrayList<JButton> listeNum = new ArrayList<JButton>();
	
	private Boolean flagPoint = false;
	
	private Boolean flagEg = false;
	
	private Boolean flagRes = false;
	
	private Boolean flagOp = false;
	
	private Double result = 0.0;
	
	private Double temp = 0.0;
	
	private String operator = "";
	
	
	public Calculatrice () {		
		//panel screen
		JPanel p_screen = new JPanel();
		p_screen.add(textField);
		
		//panel numbers
		JPanel p_numbers = new JPanel();
		p_numbers.setLayout(new GridLayout(4,3));
		
		for (int i = 9; i >= 1 ; i--) {
			listeNum.add(new JButton("" + i + ""));
			int j=9-i;
			p_numbers.add(listeNum.get(j));
			listeNum.get(j).addActionListener(new NumberListener());
		}
		
		JButton n_b0 = new JButton("0");
		n_b0.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent z) {
				// TODO Auto-generated method stub
				if (!textField.getText().equals("0") && !flagRes && !textField.getText().equals("-0")) {
					String strNumber = ((JButton)z.getSource()).getText();
					strNumber = textField.getText() + strNumber;
					textField.setText(strNumber);
				}
				else if (textField.getText().equals("0") && !flagRes) {
				}
				
				else if (flagRes) {
					del();
					String strNumber = ((JButton)z.getSource()).getText();
					strNumber = textField.getText() + strNumber;
					textField.setText(strNumber);
					flagOp = true;
				}
			}
		});
		
		JButton n_bpoint = new JButton(".");
		n_bpoint.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent p) {
				// TODO Auto-generated method stub
				if (!flagPoint && !flagRes) {
					String strNumber = ((JButton)p.getSource()).getText();
					strNumber = textField.getText() + strNumber;
					textField.setText(strNumber);
					flagPoint = true;
					flagOp = true;
				} else if (flagRes) {
					del();
					String strNumber = ((JButton)p.getSource()).getText();
					strNumber = textField.getText() + strNumber;
					textField.setText(strNumber);
					flagPoint = true;
					flagOp = true;
				}
				
			}
		});
		
		JButton n_bequals = new JButton("=");
		n_bequals.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent eq) {
				// TODO Auto-generated method stub
				
				if (flagEg) {
					myequals();
				}
			}
		});
		
		p_numbers.add(n_b0);
		p_numbers.add(n_bpoint);
		p_numbers.add(n_bequals);
		
		//panel operations
		JPanel p_operations = new JPanel();
		p_operations.setLayout(new GridLayout(5,1));
		
		JButton o_bdel = new JButton("C");
		o_bdel.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent ad) {
				// TODO Auto-generated method stub
				//if (!flagEg) {
					del();
				//}
			}
		});
		
		JButton o_bplus = new JButton("+");
		o_bplus.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent ad) {
				// TODO Auto-generated method stub
				if (flagOp) {
					addition();
					//flagEg = false;
				}
			}
		});
		
		JButton o_bminus = new JButton("-");
		o_bminus.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent ad) {
				// TODO Auto-generated method stub
				if (flagOp) {
					substract();
				}
				else if (textField.getText().isEmpty()) {
					String strNumber = ((JButton)ad.getSource()).getText();
					strNumber = textField.getText() + strNumber;
					textField.setText(strNumber);
				}
				else if (textField.getText().equals("-")) {
					textField.setText("");
				}
				
			}
		});
		
		JButton o_bdiv = new JButton("/");
		o_bdiv.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent ad) {
				// TODO Auto-generated method stub
				if (flagOp) {
					modulo();
				}
			}
		});
		
		JButton o_bmulti = new JButton("*");
		o_bmulti.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent ad) {
				// TODO Auto-generated method stub
				if (flagOp) {
					multiply();
				}
			}
		});

		p_operations.add(o_bdel);
		p_operations.add(o_bplus);
		p_operations.add(o_bminus);
		p_operations.add(o_bdiv);
		p_operations.add(o_bmulti);
		
		container.setLayout(new BorderLayout());
		container.add(p_screen, BorderLayout.NORTH);
		container.add(p_numbers, BorderLayout.WEST);
		container.add(p_operations, BorderLayout.EAST);
		
		frame.add(container);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(178, 200);
		frame.setTitle("Calculette");
		frame.setResizable(false);
		
		frame.setVisible(true);	
	}
	
	public class NumberListener implements ActionListener {
		
		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			if (!flagRes) {
				String strNumber = ((JButton)e.getSource()).getText();
				strNumber = textField.getText() + strNumber;
				textField.setText(strNumber);
				flagOp = true;
			} else if (flagRes) {
				del();
				String strNumber = ((JButton)e.getSource()).getText();
				strNumber = textField.getText() + strNumber;
				textField.setText(strNumber);
				flagOp = true;
			}
		}
	}
	
	public void myequals() {
		// TODO Auto-generated method stub

		if (operator.equals("+")) {
			result = temp + Double.parseDouble(textField.getText());
		} else if (operator.equals("-")) {
			result = temp - Double.parseDouble(textField.getText());
		} else if (operator.equals("/")) {
			result = temp / Double.parseDouble(textField.getText());
		} else if (operator.equals("*")) {
			result = temp * Double.parseDouble(textField.getText());
		}
		textField.setText(String.valueOf(result));
		flagEg = false;
		flagPoint = false;
		flagRes = true;
	}
	
	public void addition() {
		flagEg = true;
		flagPoint = false;
		flagRes = false;
		operator = "+";
		temp = Double.parseDouble(textField.getText());
		textField.setText("");
	}
	
	public void substract() {
		flagEg = true;
		flagPoint = false;
		flagRes = false;
		operator = "-";
		temp = Double.parseDouble(textField.getText());
		textField.setText("");
	}
	
	public void modulo() {
		flagEg = true;
		flagPoint = false;
		flagRes = false;
		operator = "/";
		temp = Double.parseDouble(textField.getText());
		textField.setText("");
	}
	
	public void multiply() {
		flagEg = true;
		flagPoint = false;
		flagRes = false;
		operator = "*";
		temp = Double.parseDouble(textField.getText());
		textField.setText("");
	}
	
	public void del() {
		flagEg = false;
		flagPoint = false;
		flagRes = false;
		flagOp = false;
		operator = "";
		temp = 0.0;
		textField.setText("");
	}
}
